using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Weight.Base;

namespace LED
{
    public class T4 : ILED
    {
        /// <summary>
        ///通讯设置结构体
        /// </summary>
        public LedDll.COMMUNICATIONINFO CommunicationInfo = new LedDll.COMMUNICATIONINFO();

        /// <summary>
        /// 区域坐标属性结构体变量
        /// </summary>
        public LedDll.AREARECT AreaRect = new LedDll.AREARECT();

        /// <summary>
        /// 文字属性
        /// </summary>
        public LedDll.FONTPROP FontProp = new LedDll.FONTPROP();

        /// <summary>
        /// 页面显示的属性结构体
        /// </summary>
        public LedDll.PLAYPROP PlayProp = new LedDll.PLAYPROP();

        /// <summary>
        /// 互斥锁
        /// </summary>
        private static object mObj = new object();

        /// <summary>
        /// 图片互斥锁
        /// </summary>
        private static object mImgObj = new object();

        /// <summary>
        /// 屏幕宽
        /// </summary>
        private int mWidth;
        /// <summary>
        /// 屏幕高
        /// </summary>
        private int mHeight;

        /// <summary>
        /// 字体大小
        /// </summary>
        private int mFontSize;

        /// <summary>
        /// 屏幕类型
        /// </summary>
        private int mColorType = 1;

        /// <summary>
        /// 特技
        /// </summary>
        private int mAction = 0;

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public ResultEntity Open(InitParam parameter)
        {
            var result = ResultEntity.Default();
            try
            {
                if (parameter == null) return ResultEntity.Error("LED(T4)大屏打开失败，原因:无打开设备的参数!");
                mWidth = parameter.Width;
                mHeight = parameter.Height;
                mFontSize = parameter.FontSize;
                mAction = parameter.Action;

                //设为固定IP通讯模式，即TCP通讯
                CommunicationInfo.SendType = 0;
                //给IpStr赋值LED控制卡的IP
                CommunicationInfo.IpStr = parameter.Ip;
                //LED屏号为1，注意socket通讯和232通讯不识别屏号，默认赋1就行了，485必需根据屏的实际屏号进行赋值
                CommunicationInfo.LedNumber = 1;
                //设置屏参，屏的颜色为2即为双基色，64为屏宽点数，32为屏高点数，具体函数参数说明见函数声明注示
                int nResult = LedDll.LV_SetBasicInfo(ref CommunicationInfo, mColorType, parameter.Width, parameter.Height);
                if (nResult != 0)
                {
                    //如果失败则可以调用LV_GetError获取中文错误信息
                    string ErrStr;
                    ErrStr = LedDll.LS_GetError(nResult);
                    result = ResultEntity.Error("LED(T4)大屏打开设失败,原因:"+ErrStr);
                }
                else
                {
                    AreaRect.left = 0;
                    AreaRect.top = 0;
                    AreaRect.width =parameter.Width;
                    AreaRect.height = parameter.Height;

                    FontProp.FontName = "宋体";
                    FontProp.FontSize = parameter.FontSize;
                    FontProp.FontColor = LedDll.COLOR_RED;
                    FontProp.FontBold = 0;

                    PlayProp.InStyle = mAction;
                    PlayProp.DelayTime = 10;
                    PlayProp.Speed = 2;
                    result = ResultEntity.Success("LED(T4)打开成功!");
                }
            }
            catch(Exception ex)
            {
                result = ResultEntity.Error("LED(T4)打开异常,异常信息:"+ex.Message+","+ex.StackTrace);
            }
            return result;
        }

        
        /// <summary>
        /// 发送内容到屏幕
        /// </summary>
        /// <param name="Content">发送消息的内容</param>
        /// <returns></returns>
        public ResultEntity Show(string content)
        {
            var result = ResultEntity.Default();
            lock (mObj)
            {
                try
                {
                    string str = content;
                    int nResult;
                    //节目句柄
                    int hProgram;
                    //根据传的参数创建节目句柄，64是屏宽点数，32是屏高点数，2是屏的颜色，注意此处屏宽高及颜色参数必需与设置屏参的屏宽高及颜色一致，否则发送时会提示错误
                    hProgram = LedDll.LV_CreateProgram(mWidth, mHeight, mColorType);
                    //此处可自行判断有未创建成功，hProgram返回NULL失败，非NULL成功,一般不会失败
                    nResult = LedDll.LV_AddProgram(hProgram, 1, 0, 1);//添加一个节目，参数说明见函数声明注示
                    if (nResult != 0)
                    {
                        string ErrStr = LedDll.LS_GetError(nResult);
                        result = ResultEntity.Error("LED(T4)添加节目失败，原因:"+ErrStr);
                    }

                    if (str.Contains("欢迎"))
                    {
                        AreaRect.top = 2;
                        //int days = DateTime.Now.Day;
                        //int mod = days / 10 ;
                        //if (mod == 0)
                        //{
                        //    AreaRect.top = 2;
                        //}
                        //else if (mod == 1)
                        //{
                        //    AreaRect.top = 18;
                        //}
                        //else if (mod == 2)
                        //{
                        //    AreaRect.top = 34;
                        //}

                        AreaRect.height = 16;

                        LedDll.LV_AddImageTextArea(hProgram, 1, 1, ref AreaRect, 0);
                        PlayProp.InStyle = 6;
                        PlayProp.OutStyle = 6;
                        PlayProp.Speed = 8;
                        PlayProp.DelayTime = 7;
                    }
                    else
                    {
                        AreaRect.top = 0;
                        AreaRect.left = 0;
                        AreaRect.width = mWidth;
                        AreaRect.height = mHeight;
                        LedDll.LV_AddImageTextArea(hProgram, 1, 1, ref AreaRect, 0);
                        PlayProp.InStyle = mAction;
                        PlayProp.DelayTime = 3;
                        PlayProp.Speed = 2;
                    }
                    if (!str.Contains("欢迎"))
                    {
                        //可以添加多个子项到图文区，如下添加可以选一个或多个添加
                        nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, 1, 1, LedDll.ADDTYPE_STRING, str, ref FontProp, ref PlayProp, 0, 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示
                    }
                    else
                    {
                        nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, 1, 1, LedDll.ADDTYPE_STRING, str, ref FontProp, ref PlayProp, 0, 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示
                    }
                    
                    //发送节目，此发送为一对一发送
                    nResult = LedDll.LV_Send(ref CommunicationInfo, hProgram);
                    //删除节目内存对象，防止内存泄漏
                    LedDll.LV_DeleteProgram(hProgram);
                    if (nResult != 0)
                    {
                        //如果失败则可以调用LV_GetError获取中文错误信息
                        string ErrStr;
                        ErrStr = LedDll.LS_GetError(nResult);
                        result = ResultEntity.Error("LED(T4)发送节目失败，错误原因:"+ErrStr);
                    }
                    else
                    {
                        result = ResultEntity.Success("LED(T4)发送节目成功！");
                    }
                }
                catch(Exception ex)
                {
                    result = ResultEntity.Success("LED(T4)发送节目异常，异常信息:"+ex.Message+","+ex.StackTrace);
                }
            }

            return result;
        }



        /// <summary>
        /// 发送图片到屏幕
        /// </summary>
        /// <param name="imgPath">图片路径</param>
        /// <returns></returns>
        public ResultEntity ShowImg(string imgPath)
        {
            ResultEntity result = ResultEntity.Default();
            lock(mImgObj)
            {
                try
                {
                    ///创建节目
                    int hProgram = LedDll.LV_CreateProgram(mWidth, mHeight, mColorType);
                    ///添加节目
                    int nResult = LedDll.LV_AddProgram(hProgram, 1, 0, 1);
                    if(nResult != 0)
                    {
                        result = ResultEntity.Error("LED(T4)发送节目失败，错误原因:"+ LedDll.LS_GetError(nResult));
                    }
                    else
                    {
                        //区域坐标属性结构体变量
                        LedDll.AREARECT AreaRect = new LedDll.AREARECT();
                        AreaRect.left = 0;
                        AreaRect.top = 0;
                        AreaRect.width = 96;
                        AreaRect.height = 64;
                        //定义图片区域
                        LedDll.LV_AddImageTextArea(hProgram, 1, 1, ref AreaRect, 0);
                        //播放属性结构变量
                        LedDll.PLAYPROP PlayProp = new LedDll.PLAYPROP();
                        PlayProp.InStyle = 0;
                        PlayProp.DelayTime = 3;
                        PlayProp.Speed = 4;
                        //添加图片到图片区域
                        nResult = LedDll.LV_AddFileToImageTextArea(hProgram, 1, 1, imgPath, ref PlayProp);
                        //发送
                        nResult = LedDll.LV_Send(ref CommunicationInfo, hProgram);
                        //销毁节目防止内存泄漏
                        LedDll.LV_DeleteProgram(hProgram);

                        if(nResult == 0)
                        {
                            result = ResultEntity.Success("LED(T4)发送图片节目成功");
                        }
                        else
                        {
                            result = ResultEntity.Error("LED(T4)发送图片节目失败,失败原因:"+ LedDll.LS_GetError(nResult));
                        }
                    }

                }
                catch (Exception ex)
                {
                    result = ResultEntity.Success("LED(T4)发送图片节目异常，异常信息:" + ex.Message + "," + ex.StackTrace);
                }
            }

            return result;
        }

        /// <summary>
        /// 注销初始化
        /// </summary>
        /// <returns></returns>
        public ResultEntity Close()
        {
            return ResultEntity.Success("LED(T4)不支持注销功能!");
        }
    }
}


